package com.work.saturn.account;

import android.os.Bundle;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.BaseBean;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.http.RequestSuccess;
import com.work.saturn.R;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author YaoChen
 */
public class CreateTokenActivity extends BaseHeadActivity {
    /**
     * 代币名称
     */
    @BindView(R.id.token_name)
    EditText tokenName;
    /**
     * 代币单位
     */
    @BindView(R.id.token_symbol)
    EditText tokenSymbol;
    /**
     * 代币精度
     */
    @BindView(R.id.token_decimol)
    EditText tokenDecimol;
    /**
     * 代币金额
     */
    @BindView(R.id.token_supply)
    EditText tokenSupply;
    /**
     * 描述
     */
    @BindView(R.id.token_introduction)
    EditText tokenIntroduction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_token);
        ButterKnife.bind(this);
        setTitle("CREATE　TOKEN");
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        String name = tokenName.getText().toString();
        String symbol = tokenSymbol.getText().toString();
        String dcimol = tokenDecimol.getText().toString();
        String supply = tokenSupply.getText().toString();
        String introduction = tokenIntroduction.getText().toString();
        if (name.length() == 0) {
            toast("Name不能为空");
            return false;
        }
        if (symbol.length() == 0) {
            toast("Symbol不能为空");
            return false;
        }
        if (dcimol.length() == 0) {
            toast("Dcimol不能为空");
            return false;
        }
        if (supply.length() == 0) {
            toast("Supply不能为空");
            return false;
        }
        if (introduction.length() == 0) {
            toast("Introduction不能为空");
            return false;
        }
        return true;
    }

    @OnClick(R.id.token_submit)
    public void onViewClicked() {
        if (isCheck()) {
            submitToken();
        }
    }

    /**
     * 创建token
     */
    private void submitToken() {
        HashMap<String, String> params = new HashMap<>();
        params.put("name", tokenName.getText().toString());
        params.put("symbol", tokenSymbol.getText().toString());
        params.put("decimals", tokenDecimol.getText().toString());
        params.put("amount", tokenSupply.getText().toString());
        new NetworkRequest(new RequestSuccess() {
            @Override
            public void loadHttp(Object object, String data) {
                if (object instanceof BaseBean) {
                    BaseBean baseBean = (BaseBean) object;
                    toast(baseBean.getMessage());
                    if (baseBean.isSuccess()) {
                      finish();
                    }
                }
            }
        }).requestPost(BaseUrl.CREATTOKEN,
                this, JSON.toJSONString(params), BaseBean.class, true, false);
    }
}
